package com.ruoyi.system.service.impl;

import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.PinyinUtil;
import com.ruoyi.system.domain.vo.DistrictVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysDistrictMapper;
import com.ruoyi.system.domain.SysDistrict;
import com.ruoyi.system.service.ISysDistrictService;

/**
 * 四级地域Service业务层处理
 *
 * @author MagicCube
 * @date 2023-05-10
 */
@Service
public class SysDistrictServiceImpl implements ISysDistrictService
{
    @Autowired
    private SysDistrictMapper sysDistrictMapper;

    /**
     * 查询四级地域
     *
     * @param id 四级地域主键
     * @return 四级地域
     */
    @Override
    public SysDistrict selectSysDistrictById(Long id)
    {
        return sysDistrictMapper.selectSysDistrictById(id);
    }
    /**
     * 查询四级地域
     *
     * @param cityName 城市名称
     * @return 四级地域
     */
    @Override
    public SysDistrict selectSysDistrictByName(String cityName) {
        return sysDistrictMapper.selectSysDistrictByName(cityName);
    }

    @Override
    public Map<Long, List<DistrictVo>> selectAllDistrictList(SysDistrict sysDistrict) {
        List<DistrictVo> districtList = sysDistrictMapper.selectSysAllProvinceList(sysDistrict);

        Map<Integer, List<DistrictVo>> district = districtList.stream().collect(Collectors.groupingBy(DistrictVo::getLevel));

        List<DistrictVo> provinceList = district.get(0);
        List<DistrictVo> cityList = district.get(1);

        //城市子集填充
        Map<Long, List<DistrictVo>> area = district.get(2).stream().collect(Collectors.groupingBy(DistrictVo::getPid));
        for (DistrictVo districtVo : cityList) {
            districtVo.setChildren(area.get(districtVo.getId()));
        }

        //省份子集填充
        Map<Long, List<DistrictVo>> city = cityList.stream().collect(Collectors.groupingBy(DistrictVo::getPid));
        for (DistrictVo districtVo : provinceList) {
            districtVo.setChildren(city.get(districtVo.getId()));
        }

        //省份分类
        return provinceList.stream().collect(Collectors.groupingBy(DistrictVo::getPid));
    }

    @Override
    public Map<Long, List<DistrictVo>> cascadeQuery() {
        List<DistrictVo> districtList = sysDistrictMapper.cascadeQuery();

        Map<Integer, List<DistrictVo>> district = districtList.stream().collect(Collectors.groupingBy(DistrictVo::getLevel));

        List<DistrictVo> provinceList = district.get(0);
        List<DistrictVo> cityList = district.get(1);

//        //城市子集填充
//        Map<Long, List<DistrictVo>> area = district.get(2).stream().collect(Collectors.groupingBy(DistrictVo::getPid));
//        for (DistrictVo districtVo : cityList) {
//            districtVo.setChildren(area.get(districtVo.getId()));
//        }

        //省份子集填充
        Map<Long, List<DistrictVo>> city = cityList.stream().collect(Collectors.groupingBy(DistrictVo::getPid));
        for (DistrictVo districtVo : provinceList) {
            districtVo.setChildren(city.get(districtVo.getId()));
        }

        //省份分类
        return provinceList.stream().collect(Collectors.groupingBy(DistrictVo::getPid));
    }

    /**
     * 查询四级地域列表
     *
     * @param sysDistrict 四级地域
     * @return 四级地域
     */
    @Override
    public List<SysDistrict> selectSysDistrictList(SysDistrict sysDistrict)
    {
        return sysDistrictMapper.selectSysDistrictList(sysDistrict);
    }

    @Override
    public Map<String, List<SysDistrict>> selectDistrictList(SysDistrict sysDistrict) {
        List<SysDistrict> sysDistricts = sysDistrictMapper.selectSysDistrictList(sysDistrict);
        for (SysDistrict district : sysDistricts) {
            district.setInitial(PinyinUtil.getPinyinInitials(district.getName()).toUpperCase().substring(0,1));
        }
        List<SysDistrict> hotDistricts = sysDistricts.stream().filter(SysDistrict -> (SysDistrict.getIsHot() == 1)).collect(Collectors.toList());
        sysDistricts = sysDistricts.stream().sorted(Comparator.comparing(SysDistrict::getInitial)).collect(Collectors.toList());
        Map<String, List<SysDistrict>> map = new HashMap<>();
        map.put("hotCities",hotDistricts);
        map.put("allCities",sysDistricts);
        return map;
    }

    /**
     * 新增四级地域
     *
     * @param sysDistrict 四级地域
     * @return 结果
     */
    @Override
    public int insertSysDistrict(SysDistrict sysDistrict)
    {
        sysDistrict.setCreateTime(DateUtils.getNowDate());
        return sysDistrictMapper.insertSysDistrict(sysDistrict);
    }

    /**
     * 修改四级地域
     *
     * @param sysDistrict 四级地域
     * @return 结果
     */
    @Override
    public int updateSysDistrict(SysDistrict sysDistrict)
    {
        sysDistrict.setUpdateTime(DateUtils.getNowDate());
        return sysDistrictMapper.updateSysDistrict(sysDistrict);
    }

    /**
     * 批量删除四级地域
     *
     * @param ids 需要删除的四级地域主键
     * @return 结果
     */
    @Override
    public int deleteSysDistrictByIds(Long[] ids)
    {
        return sysDistrictMapper.deleteSysDistrictByIds(ids);
    }

    /**
     * 删除四级地域信息
     *
     * @param id 四级地域主键
     * @return 结果
     */
    @Override
    public int deleteSysDistrictById(Long id)
    {
        return sysDistrictMapper.deleteSysDistrictById(id);
    }
}
